using Photon;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityStandardAssets.CrossPlatformInput;

[RequireComponent(typeof(RectTransform))]
public class Joystick : Photon.MonoBehaviour, IPointerDownHandler, IEventSystemHandler, IPointerUpHandler, IDragHandler
{
	private RectTransform joystick;

	public bool auto;

	[HideInInspector]
	public Vector3 joydelta;

	[HideInInspector]
	public Vector3 LastDelta;

	public Vector2 currpos;

	[HideInInspector]
	public float dist;

	private bool touch;

	public float radius;

	public GameObject JoyModel;

	public GameObject JoySphere;

	public string horizontalAxisName = "Horizontal";

	public string verticalAxisName = "Vertical";

	public static CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;

	public static CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis;

	private Vector3 startpos;

	private bool returnJoystick;

	void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
	{
		if (auto)
		{
			startpos = eventData.position;
			JoySphere.transform.position = startpos;
			JoyModel.SetActive(true);
			JoySphere.SetActive(true);
		}
		else
		{
			JoyModel.SetActive(true);
		}
		JoyModel.transform.position = eventData.position;
		currpos = Vector3.zero;
		returnJoystick = false;
		touch = true;
	}

	void IDragHandler.OnDrag(PointerEventData eventData)
	{
		JoyModel.transform.position = Pos(eventData.position);
	}

	void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
	{
		returnJoystick = true;
		joydelta = Vector3.zero;
		currpos = Vector2.zero;
		touch = false;
	}

	private void Start()
	{
		startpos = base.transform.position;
	}

	private void Update()
	{
		dist = ((!touch) ? 0f : ((!(currpos.x > 0f) && !(currpos.y > 0f)) ? 0f : (Vector3.Distance(startpos, currpos) / 100f)));
		if (returnJoystick)
		{
			if (auto)
			{
				JoyModel.SetActive(false);
				JoySphere.SetActive(false);
			}
			else
			{
				JoyModel.transform.position = startpos;
			}
			returnJoystick = false;
		}
		UpdateVirtualAxes(new Vector3(joydelta.x, joydelta.z, 0f));
	}

	private void OnEnable()
	{
		returnJoystick = true;
		joydelta = Vector3.zero;
		currpos = Vector2.zero;
		touch = false;
	}

	private Vector2 Pos(Vector2 postouch)
	{
		currpos = Vector2.ClampMagnitude(postouch - (Vector2)startpos, radius) + (Vector2)startpos;
		joydelta = new Vector3(currpos.x - startpos.x, 0f, currpos.y - startpos.y).normalized;
		LastDelta = joydelta;
		return currpos;
	}

	public void Reset()
	{
		JoyModel.SetActive(false);
		joydelta = Vector3.zero;
		LastDelta = Vector3.zero;
		dist = 0f;
		touch = false;
		startpos = Vector3.zero;
	}

	public void UpdateVirtualAxes(Vector3 value)
	{
		value = value.normalized;
		m_HorizontalVirtualAxis.Update(value.x);
		m_VerticalVirtualAxis.Update(value.y);
	}

	private void OnDisable()
	{
	}

	public void CreateVirtualAxes()
	{
		m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
		CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
		m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
		CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
	}
}
